package com.ljh.mall.mallseckill.schedule;

import com.ljh.mall.mallseckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author ：刘剑鸿
 * @date ：Created in 2023/5/15 23:20
 * @description： 定时上架秒杀商品信息
 * @modified By：
 * @version: $
 */
@Slf4j
@Component
public class SeckillSkuSchedule {

    @Autowired
    SeckillService seckillService;

    @Autowired
    RedissonClient redissonClient;

    @Async
    @Scheduled(cron = "*/5 * * * * *") // 每隔5秒再执行一次任务。
    public void uploadSeckillSku3Days(){
        // 调用上架商品的方法
        log.info("定时上架秒杀商品执行了...." + new Date());
        RLock lock = redissonClient.getLock("seckill:upload:lock");
        lock.lock(10, TimeUnit.SECONDS);
        try{
              seckillService.uploadSeckillSku3Days();
        } catch (Exception e){
            lock.unlock();
        }


    }
}
